package com.dsg.service.account.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.UUIDGenerator;
import com.dsg.common.util.toolbox.CollectionUtil;
import com.dsg.dao.account.DsgAccountCheckMapper;
import com.dsg.dao.account.DsgAccountLedgerMapper;
import com.dsg.dao.oil.DsgOilAccountMapper;
import com.dsg.entity.account.DsgAccountCheck;
import com.dsg.entity.account.DsgAccountLedger;
import com.dsg.entity.oil.DsgOilAccount;
import com.dsg.service.account.IDsgAccountLedgerService;
import com.dsg.util.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 油公司總帳明細 服务实现类
 * </p>
 *
 * @author system
 * @since 2020-07-12
 */
@Service
public class DsgAccountLedgerServiceImpl extends ServiceImpl<DsgAccountLedgerMapper, DsgAccountLedger> implements IDsgAccountLedgerService {

    @Autowired
    private DsgAccountLedgerMapper dsgAccountLedgerMapper;
    @Autowired
    private DsgOilAccountMapper dsgOilAccountMapper;
    @Autowired
    private DsgAccountCheckMapper dsgAccountCheckMapper;

    @Override
    public IPage<DsgAccountLedger> selectByList(Page<DsgAccountLedger> page, DsgAccountLedger dsgAccountLedger) {
        return page.setRecords(dsgAccountLedgerMapper.selectByList(page, dsgAccountLedger));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDsgAccountLedger(DsgAccountLedger dsgAccountLedger) {
        if (dsgAccountLedger != null) {
            List<DsgAccountLedger> dsgAccountLedgerList = dsgAccountLedger.getDsgAccountLedgerList();
            //查詢油公司默認賬戶信息
            DsgOilAccount dsgOilAccount = new DsgOilAccount();
            dsgOilAccount.setOilComNo(dsgAccountLedger.getOilCom());
            dsgOilAccount.setIsDefault(DsgOilAccount.AppStatus.yes.getCode());
            dsgOilAccount.setEntCode(SingletonLoginUtils.getSystemEntCode());
            List<DsgOilAccount> dsgOilAccountList = dsgOilAccountMapper.selectByList(dsgOilAccount);
            if (CollectionUtil.isEmpty(dsgOilAccountList)) {
                throw new RRException("油公司默認賬戶不存！");
            }
            //新增支票
            dsgOilAccount = dsgOilAccountList.get(0);
            if (CollectionUtil.isNotEmpty(dsgAccountLedgerList)) {
                for (DsgAccountLedger dal : dsgAccountLedgerList) {
                    dal.setOid(UUIDGenerator.getUUID());
                    dal.setAccMode(DsgAccountLedger.AppStatus.temporary.getCode());
                    //回寫油公司默認賬戶信息
                    dal.setAccountCode(dsgOilAccount.getAccountNo());
                    dal.setAccountName(dsgOilAccount.getAccountName());
                    dal.setTranItem(DsgAccountLedger.AppStatus.check.getCode());//支票
                    dal.setOutAmount(BigDecimal.ZERO);//支票時支出默認為0
                    dal.setIncludeIn(DsgAccountLedger.AppStatus.notCounting.getCode());
                    dal.setRecStatus(DsgAccountLedger.AppStatus.suspenseAccount.getCode());
                }
                saveBatch(dsgAccountLedgerList);
                //寫支票信息
                DsgAccountCheck dsgAccountCheck = new DsgAccountCheck();
                dsgAccountCheck.setOid(UUIDGenerator.getUUID());
                dsgAccountCheck.setCheckNo(dsgAccountLedgerList.get(0).getCheckNo());
                dsgAccountCheck.setCheckSum(dsgAccountLedgerList.get(0).getCheckSum());
                dsgAccountCheck.setPreSum(BigDecimal.ZERO);
                dsgAccountCheck.setCashSum(BigDecimal.ZERO);
                dsgAccountCheckMapper.insert(dsgAccountCheck);
            } else {
                //新增修改其它
                if (dsgAccountLedger.getInAmount() == null && dsgAccountLedger.getOutAmount() == null) {
                    throw new RRException("收入和支出不能同時為空！");
                }
                if (dsgAccountLedger.getInAmount() != null && dsgAccountLedger.getOutAmount() != null) {
                    throw new RRException("收入和支出不能同時有值！");
                }
                dsgAccountLedger.setOid(UUIDGenerator.getUUID());
                dsgAccountLedger.setAccMode(DsgAccountLedger.AppStatus.temporary.getCode());
                //回寫油公司默認賬戶信息
                dsgAccountLedger.setInAmount(dsgAccountLedger.getInAmount() == null ? BigDecimal.ZERO : dsgAccountLedger.getInAmount());
                dsgAccountLedger.setOutAmount(dsgAccountLedger.getOutAmount() == null ? BigDecimal.ZERO : dsgAccountLedger.getOutAmount());
                dsgAccountLedger.setAccountCode(dsgOilAccount.getAccountNo());
                dsgAccountLedger.setAccountName(dsgOilAccount.getAccountName());
                dsgAccountLedger.setTranItem(DsgAccountLedger.AppStatus.cash.getCode());//現金
                dsgAccountLedger.setIncludeIn(DsgAccountLedger.AppStatus.notCounting.getCode());
                dsgAccountLedger.setRecStatus(DsgAccountLedger.AppStatus.suspenseAccount.getCode());
                dsgAccountLedgerMapper.insert(dsgAccountLedger);
            }
        }
    }
}
